/**
 * 
 */
package org.dbwebx.gwt.mvp.view;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;
import com.gwtext.client.core.Margins;
import com.gwtext.client.core.RegionPosition;
import com.gwtext.client.widgets.HTMLPanel;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.TabPanel;
import com.gwtext.client.widgets.Toolbar;
import com.gwtext.client.widgets.ToolbarItem;
import com.gwtext.client.widgets.Viewport;
import com.gwtext.client.widgets.layout.AccordionLayout;
import com.gwtext.client.widgets.layout.BorderLayout;
import com.gwtext.client.widgets.layout.BorderLayoutData;
import com.gwtext.client.widgets.layout.FitLayout;

/**
 * @author Izzet_Mustafayev
 * 
 */
public class ApplicationTemplateView extends Composite implements
		IApplicationTemplateView {

	/**
	 * The DbWebX application main panel.
	 */

	/**
	 * Creates application template.
	 */
	public ApplicationTemplateView() {
		initWidget(prepareLayouts());
	}

	private Panel prepareLayouts() {
		Panel panel = new Panel();
		panel.setBorder(false);
		panel.setPaddings(15);
		panel.setLayout(new FitLayout());

		borderPanel = new Panel();
		borderPanel.setLayout(new BorderLayout());

		headerPanel = new Toolbar();
		headerPanel.setEl(new HTML("<p>north - generally for menus, toolbars"
				+ " and/or advertisements</p>").getElement());
		headerPanel.setHeight(32);
		borderPanel
				.add(headerPanel, new BorderLayoutData(RegionPosition.NORTH));

		// add south panel
		Panel southPanel = new HTMLPanel(
				"<p>south - generally for informational stuff,"
						+ " also could be for status bar</p>");
		southPanel.setHeight(100);
		southPanel.setCollapsible(true);
		southPanel.setTitle("South");

		BorderLayoutData southData = new BorderLayoutData(RegionPosition.SOUTH);
		southData.setMinSize(100);
		southData.setMaxSize(200);
		southData.setMargins(new Margins(0, 0, 0, 0));
		southData.setSplit(true);
		borderPanel.add(southPanel, southData);

		// add east panel
		Panel eastPanel = new Panel();
		eastPanel.setTitle("East Side");
		eastPanel.setCollapsible(true);
		eastPanel.setCollapsed(true);
		eastPanel.setWidth(225);
		eastPanel.setLayout(new FitLayout());

		BorderLayoutData eastData = new BorderLayoutData(RegionPosition.EAST);
		eastData.setSplit(true);
		eastData.setMinSize(175);
		eastData.setMaxSize(400);
		eastData.setMargins(new Margins(0, 0, 5, 0));

		borderPanel.add(eastPanel, eastData);

		westPanel = new Panel();
		westPanel.setTitle("Manage Panel");
		westPanel.setCollapsible(true);
		westPanel.setWidth(200);
		westPanel.setLayout(new AccordionLayout(true));
		Panel sourceNodePanel = new Panel();
		sourceNodePanel.setTitle("Source Nodes");
		sourceNodePanel.setBorder(false);
		sourceNodePanel.setIconCls("settings-icon");
		sourceNodePanel.setCollapsed(false);
		westPanel.add(sourceNodePanel);

		BorderLayoutData westData = new BorderLayoutData(RegionPosition.WEST);
		westData.setSplit(true);
		westData.setMinSize(175);
		westData.setMaxSize(400);
		westData.setMargins(new Margins(0, 5, 0, 0));

		borderPanel.add(westPanel, westData);

		TabPanel centerPanel = new TabPanel();
		centerPanel.setDeferredRender(false);
		centerPanel.setActiveTab(0);

		Panel centerPanelTwo = new HTMLPanel();
		centerPanelTwo.setHtml("<p>Some work data here </p>");
		centerPanelTwo.setTitle("Work Panel");
		centerPanelTwo.setAutoScroll(true);

		centerPanel.add(centerPanelTwo);
		borderPanel.add(centerPanel,
				new BorderLayoutData(RegionPosition.CENTER));

		panel.add(borderPanel);
		borderPanel.setHeight("100%");
		borderPanel.setHeader(true);
		new Viewport(panel);
		return panel;
	}

	private Toolbar headerPanel;
	private Panel borderPanel;
	private Panel westPanel;

	public void addHeader(final Widget widget) {
		headerPanel.addItem(new ToolbarItem(widget.getElement()));
	}

	public void addWest(final Widget widget) {
		Panel sourceNodePanel = (Panel) westPanel.getComponent(0);
		sourceNodePanel.add(widget);
		sourceNodePanel.syncSize();
		westPanel.syncSize();
	}

	public void addEast(final Widget widget) {
		// applicationPanel.add(widget, DockPanel.EAST);
	}

	public void addContent(final Widget widget) {
		// applicationPanel.add(widget, DockPanel.CENTER);
	}
}
